package com.hmall;


import com.alibaba.fastjson.JSON;
import com.hmall.common.myClient.ItemClient;
import com.hmall.common.pojo.Item;
import com.hmall.common.pojo.PageDTO;
import com.hmall.config.EsConstant;
import com.hmall.pojo.ItemDoc;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * @Author 15198
 * @Date 2022/8/10 16:56
 * @Description:
 * @Created by
 */
@SpringBootTest
public class MyFeignApi {

    @Autowired
    private ItemClient itemClient;
    @Autowired
    private RestHighLevelClient Client;

    @Test
    public void testQueryItem() throws Exception {
        // http://itemservice/item/list?page=1&size=5
        int page = 1;
        while (true) {
            PageDTO<Item> pageDTO = itemClient.page(page, 2000);
            List<Item> list = pageDTO.getList();

            BulkRequest bulkRequest = new BulkRequest();
            if (list.size()<=0){
                break;
            }
            for (Item item : list) {
                ItemDoc itemDoc = new ItemDoc(item);
                String jsonString = JSON.toJSONString(itemDoc);

                bulkRequest.add(new IndexRequest(EsConstant.INDEX)
                        .id(itemDoc.getId().toString())
                        .source(jsonString, XContentType.JSON));
            }
            Client.bulk(bulkRequest, RequestOptions.DEFAULT);
            page++;
        }
    }
 }


